﻿using System;
using System.Collections.Generic;
using System.Text;
using DSAClient.Models;
using DSAClient.Models.Input;
using DSAClient.Models.Output;
using DSAClient.Utils;

namespace DSAClient.Core
{
    public class Api
    {
        private static string host = "http://api.mzc9.com/api/client";
        //private static string host = "http://localhost/api/client";
        /// <summary>
        /// 获取淘宝订单信息
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult<List<TaobaoSunOrderInfo>> GetTaobaoOrder(TaobaoOrderGetPara para)
        {
            return post<InvokeResult<List<TaobaoSunOrderInfo>>>("GetTaobaoOrder", para);
        }


        /// <summary>
        /// 获取照片尺寸数据
        /// </summary>
        /// <returns></returns>
        public static InvokeResult<List<PhotoSizeInfo>> QueryPhotoSize()
        {
            return post<InvokeResult<List<PhotoSizeInfo>>>("QueryPhotoSize", "");
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <returns></returns>
        public static InvokeResult<List<ConfigItem>> GetConfig()
        {
            return post<InvokeResult<List<ConfigItem>>>("GetConfig", "");
        }

        /// <summary>
        /// 保存订单
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult SaveOrder(OrderSubmitInfo para)
        {
            return post<InvokeResult>("SaveOrder", para);
        }

        /// <summary>
        /// 保存订单图片数据
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult SaveOrderImage(ImageSubmitInfo para)
        {
            return post<InvokeResult>("SaveOrderImage", para);
        }

        public static InvokeResult UpdateTrade(string para)
        {
            return post<InvokeResult>("UpdateTrade", para);
        }

        public static InvokeResult UpdateTrade(object para)
        {
            return post<InvokeResult>("UpdateTrade", para);
        }

        /// <summary>
        /// 与服务器交互方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="para"></param>
        /// <returns></returns>
        static T post<T>(string cmd, object para) where T : InvokeResult, new()
        {

            T result = default(T);
            string url = string.Format("{0}/{1}", host, cmd);
            HttpHelper httpHelper = new HttpHelper();
            string postdata = "data=" + JsonHelper.SerializeObject(para);
            string html = httpHelper.GetHtml(url, postdata, true);
            if (string.IsNullOrEmpty(html))
            {
                result = new T();
                result.msg = "连接服务器失败.";
            }
            else
            {
                try
                {
                    result = JsonHelper.DeserializeObject<T>(html);
                }
                catch (Exception ex)
                {

                    result = new T();
                    result.msg = "服务器返回的数据无法识别!";
                }
            }

            return result;
        }
    }
}
